﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using GalaSoft.MvvmLight.Command;
using Rio.Framework.Pages;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls.BaseClass;
using Rio.Manager.Application.ViewModel.BaseClass;

namespace Rio.Manager.Application.ViewModel.GeneralTabs
{
    public class CustomerTabViewModel : TabViewModelBase
    {
        #region Fields

        private AdministrationCustomerControlBase _administrationCustomerControl;
        private int _loadingPercent;

        #endregion

        #region Properties

        public RelayCommand InitializationCommand { get; set; }
        public string XAPSource { get; private set; }

        public int LoadingPercent
        {
            get { return _loadingPercent; }
            set
            {
                _loadingPercent = value;
                RaisePropertyChanged("LoadingPercent");
                RaisePropertyChanged("LoadingMessage");
            }
        }

        [Import(typeof (AdministrationCustomerControlBase))]
        public AdministrationCustomerControlBase AdministrationCustomerControl
        {
            get { return _administrationCustomerControl; }
            set
            {
                _administrationCustomerControl = value;
                RaisePropertyChanged("AdministrationCustomerControl");
            }
        }

        public string LoadingMessage
        {
            get { return String.Format("Chargement de l'interface client : {0} % ...", LoadingPercent); }
        }

        #endregion

        public override void InitCommands()
        {
            InitializationCommand = new RelayCommand(Initialization);
        }

        public override void InitLabels() {}

        private void Initialization()
        {
        }

        private void ExternalXapDownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            LoadingPercent = e.ProgressPercentage;
        }

        private void ExternalXapDownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            LoadingPercent = 100;

            if (e.Error != null)
            {
                ShowModalDialog("Error", "Error while loading customer xap.", MessageBoxButtons.Ok, MessageBoxIcon.Error);
            }
            else
            {
                CompositionInitializer.SatisfyImports(this);
            }
        }
    }
}